package com.ygqh.baby.controller.mobile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.QrCodeType;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgQrCodeService;
import com.ygqh.baby.utils.SessionUtil;

/**
 * 微信相关接口
 * 
 * @author guohao
 *
 */
@Controller
@RequestMapping("/app/qrcode")
public class YgQrCodeController extends BaseController {

	@Autowired
	private YgQrCodeService ygQrCodeService;
	@Value("${promotion.link}")
	protected String promotionLink;

	@RequestMapping(value = "getQrCodeForInvite", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity getQrCodeForInvite(@RequestParam(required = true) String inviteCode, HttpServletRequest request) throws Exception {
		YgUser user = SessionUtil.getCurrentUser();
		String url = promotionLink + "?sourceCode" + user.getSourceCode() + "&inviteCode" + inviteCode;
		File createQrCodeFile = ygQrCodeService.createQrCodeFile(url, QrCodeType.UserAgentInvite, QrCodeType.UserAgentInvite + "_" + inviteCode+".jpg");
		String qrcodeUrl = ygQrCodeService.uploadQrCodeToAliYun(createQrCodeFile);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("codeUrl", qrcodeUrl);
		return ResponseEntity.success(map);
	}

}
